# this code reproduces Figure A3 in SM Part C
# Gschwend, M�ller, Munzert, Neunhoeffer, Stoetzer 2021
# 'The Zweitstimme Model: A Dynamic Forecast of the 2021 German Federal Election'

# load district predictions

res_list <- readRDS("../processed-data/2021_wkr.RDS")
nsim <- 2000
mu_nsim <- 25

winner <-
  matrix(
    unlist(lapply(res_list, "[[", "winner_nn")),
    nrow = nsim * mu_nsim,
    ncol = 299,
    byrow = T
  )

# get winning probabilities

win_probs <- apply(winner, 2, function(x)
  - sort(-table(x) / nrow(winner)))

# define colors

plot_col <- rgb(0, 0, 0, maxColorValue = 255)

# load mapping data

wkr17 <- rgdal::readOGR(dsn = "../raw-data/shp", stringsAsFactors = F)

tiff(
    "../figures/figure_A3.tiff",
    height = 9*0.6,
    width = 16*0.6 ,
    units = "in",
    res = 300
  )

par(mfrow = c(2, 3),
    oma = c(0, 0, 4, 0),
    mar = c(0.5,0.5,2.5,0.5),
    lheight = 1)

# CDU
col_vec <- NULL

for (i in 1:299) {
  tmp <- win_probs[[i]]
  if ("CDU" %in% names(tmp) | "CSU" %in% names(tmp)) {
    col_vec <-
      c(col_vec, adjustcolor(plot_col, alpha = tmp[names(tmp) == "CDU" |
                                                     names(tmp) == "CSU"]))
  }
  else{
    col_vec <- c(col_vec, NA)
  }
}

plot(
  wkr17,
  col = col_vec,
  border = "darkgrey",
  lwd = 0.2,
  main = "CDU/CSU"
)

# SPD
col_vec <- NULL

for (i in 1:299) {
  tmp <- win_probs[[i]]
  if ("SPD" %in% names(tmp)) {
    col_vec <-
      c(col_vec, adjustcolor(plot_col, alpha = tmp[names(tmp) == "SPD"]))
  }
  else{
    col_vec <- c(col_vec, NA)
  }
}

plot(
  wkr17,
  col = col_vec,
  border = "darkgrey",
  lwd = 0.2,
  main = "SPD"
)

# AFD
col_vec <- NULL

for (i in 1:299) {
  tmp <- win_probs[[i]]
  if ("AFD" %in% names(tmp)) {
    col_vec <-
      c(col_vec, adjustcolor(plot_col, alpha = tmp[names(tmp) == "AFD"]))
  }
  else{
    col_vec <- c(col_vec, NA)
  }
}

plot(
  wkr17,
  col = col_vec,
  border = "darkgrey",
  lwd = 0.2,
  main = "AfD"
)

# FDP
col_vec <- NULL

for (i in 1:299) {
  tmp <- win_probs[[i]]
  if ("FDP" %in% names(tmp)) {
    col_vec <-
      c(col_vec, adjustcolor(plot_col, alpha = tmp[names(tmp) == "FDP"]))
  }
  else{
    col_vec <- c(col_vec, NA)
  }
}

plot(
  wkr17,
  col = col_vec,
  border = "darkgrey",
  lwd = 0.2,
  main = "FDP"
)

# LINKE
col_vec <- NULL

for (i in 1:299) {
  tmp <- win_probs[[i]]
  if ("LINKE" %in% names(tmp)) {
    col_vec <-
      c(col_vec, adjustcolor(plot_col, alpha = tmp[names(tmp) == "LINKE"]))
  }
  else{
    col_vec <- c(col_vec, NA)
  }
}

plot(
  wkr17,
  col = col_vec,
  border = "darkgrey",
  lwd = 0.2,
  main = "LINKE"
)

# GREENS
col_vec <- NULL

for (i in 1:299) {
  tmp <- win_probs[[i]]
  if ("GRUENE" %in% names(tmp)) {
    col_vec <-
      c(col_vec, adjustcolor(plot_col, alpha = tmp[names(tmp) == "GRUENE"]))
  }
  else{
    col_vec <- c(col_vec, NA)
  }
}

plot(
  wkr17,
  col = col_vec,
  border = "darkgrey",
  lwd = 0.2,
  main = "GREENS"
)

mtext(
  "Probabilities to win each electoral district per party \n 100 days before the election",
  outer = T,
  font = 2
)

dev.off()
